package engine;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import queries.Document;
import queries.Query;
import queries.QueryResult;
import util.Logger;

/**
 * Class for refining query based on user feedback
 * 
 * @author ak2765
 * @author Vika
 */
public class QueryRefiner {

	private Corpus corpus = new Corpus();
	
	/**
	 * Modify query by appending new relevant keywords
	 * 
	 * @param query
	 * @param queryResult
	 * @param selectedDocuments
	 * @return
	 */
	public Query refineQuery(Query query, QueryResult queryResult, int[] selectedDocuments) {
		this.addNegativeDocuments(queryResult.selectResultsExcluding(selectedDocuments));
		this.addPositiveDocuments(queryResult.selectResultsIncluding(selectedDocuments));

		List<String> keyWordsToAdd = new LinkedList<String>();
		
		List<String> queryKeyWords = query.getListOfWords();
		
		//queryKeyWords = sortByBiGramRelevance(queryKeyWords);
		
		List<String> newKeyWords = this.corpus.getWordsSortedByRelevance();
		
		for(int i=0;i<newKeyWords.size();i++) {
			if (i == 2) {
				break;
			}
			
			String newKeyWord = newKeyWords.get(i);
			
			if (queryKeyWords.contains(newKeyWord)) {
				continue;
			}
			
			keyWordsToAdd.add(newKeyWord);
		}
		
		
		Logger.info("Adding new key words: " + keyWordsToAdd);
		
		return query.addNewKeywords(keyWordsToAdd);
	}

	private List<String> sortByBiGramRelevance(List<String> queryKeyWords) {
		this.corpus.getTheTenPercentMorePopularBiGrams();
		
		
		return null;
	}

	/**
	 * @param selectedDocuments
	 */
	private void addPositiveDocuments(List<Document> selectedDocuments) {
		for(Document doc : selectedDocuments) {
			this.corpus.addPositiveDocument(doc);
		}
	}

	/**
	 * @param selectedDocuments
	 */
	private void addNegativeDocuments(List<Document> selectedDocuments) {
		for(Document doc : selectedDocuments) {
			this.corpus.addNegativeDocument(doc);
		}
	}

	public List<BiGram> getBiGrams() {
		return corpus.getTheTenPercentMorePopularBiGrams();
	}

}
